package com.topisv.tms.web.work.prepkg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.Servlets;

import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.work.prepkg.PrepkgPkgItem;
import com.topisv.tms.entity.work.prepkg.PrepkgPkgTask;
import com.topisv.tms.service.AreaGroupService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.work.prepkg.PrepkgPkgItemService;
import com.topisv.tms.service.work.prepkg.PrepkgPkgTaskService;

/**
 * 预包装包装—control
 * @author ch
 *
 */
@Controller
@RequestMapping(value = "/prepkgPkg")
public class PrepkgPkgController {

	private static final Logger logger = LoggerFactory.getLogger(PrepkgPkgController.class);

	@Autowired
	private CustomersService customersService;
	@Autowired
	private PrepkgPkgTaskService prepkgPkgTaskService;
	@Autowired
	private AreaGroupService areaGroupService;
	@Autowired
	private PrepkgPkgItemService prepkgPkgItemService;
	
	

	/**
	 * 导向包装任务列表页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "toPkgTaskListPage")
	public String toPkgTaskListPage(Model model, ServletRequest request) {
		try {
			
			//查出当前用户关联的商家列表
			ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			List<Customers> customerList=this.customersService.loadCustomerByAuthority(curUser);
			model.addAttribute("customerList",customerList);
			model.addAttribute("userType",curUser.userType);
		    
		    //查找我的包装任务
		    Map<String, Object> param_my=new HashMap<String, Object>();
		    param_my.put("workerUuid", curUser.jobuuid);
		    param_my.put("status", "myTask");
		    List<PrepkgPkgTask> myTaskList=this.prepkgPkgTaskService.loadTaskByParam(param_my);
		    model.addAttribute("myTaskList",myTaskList);
		    
		    //如果是paiu用户,初始化数据
			if("paiu".equals(curUser.userType)){
				//未分配的包装任务
			    Map<String, Object> param_untaked=new HashMap<String, Object>();
			    param_untaked.put("customerCodes", StringUtils.isBlank(curUser.customers)?"blank":curUser.customers);
			    param_untaked.put("areagroups", StringUtils.isBlank(curUser.areagroups)?"blank":curUser.areagroups);
			    param_untaked.put("status", "undo"); 
			    List<PrepkgPkgTask> untakedTaskList=this.prepkgPkgTaskService.loadTaskByParam(param_untaked);
			    model.addAttribute("untakedTaskList",untakedTaskList);
			}
		    
		    //如果是商家用户,初始化数据
			if("customer".equals(curUser.userType)){
				
				String customerCode=customerList.get(0).getCustomerCode();
				model.addAttribute("_customerCode",customerCode);
				
				//初始化仓库数据
				List<AreaGroup> warehouseList=this.areaGroupService.loadAreaGroupByAuthority(curUser,customerCode);
				model.addAttribute("warehouseList", warehouseList);
				
				//未分配的包装任务
			    Map<String, Object> param_untaked=new HashMap<String, Object>();
			    param_untaked.put("customerCodes", customerCode);
			    param_untaked.put("status", "undo"); 
			    List<PrepkgPkgTask> untakedTaskList=this.prepkgPkgTaskService.loadTaskByParam(param_untaked);
			    model.addAttribute("untakedTaskList",untakedTaskList);
			}
			
			
		} catch (Exception e) {
			logger.error("error",e);
		}
		return "work/prepkg/prepkgPkgTaskList";
	}
	
	/**
	 * 查询包装任务列表
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "queryPkgTaskList")
	public String queryPkgTaskList(Model model, ServletRequest request, ServletResponse response) throws Exception {
		try {
		
			//查出当前用户关联的商家列表
			ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			List<Customers> customerList=this.customersService.loadCustomerByAuthority(curUser);
			model.addAttribute("customerList",customerList);
			model.addAttribute("userType",curUser.userType);
			
			//查找我的包装任务
		    Map<String, Object> param_my=new HashMap<String, Object>();
		    param_my.put("workerUuid", curUser.jobuuid);
		    param_my.put("status", "myTask"); 
		    List<PrepkgPkgTask> myTaskList=this.prepkgPkgTaskService.loadTaskByParam(param_my);
		    model.addAttribute("myTaskList",myTaskList);
			
		    //查询未分配的包装任务
			Map<String, Object> param = Servlets.getParametersStartingWith(request, "_");
			String warehouseCode=(String) param.get("warehouseCode");
			String customerCode=(String) param.get("customerCode");
			
			if(StringUtils.isBlank(customerCode)){
				param.put("customerCodes", this.customersService.loadCustomerByAuthority(curUser.jobuuid));
			}else{
				param.put("customerCodes", customerCode);
			}
			
			if(StringUtils.isBlank(warehouseCode)){
				param.put("areagroups", curUser.areagroups);
			}else{
				param.put("areagroups", warehouseCode);
			}
			
			param.put("status", "undo"); 
			
			List<PrepkgPkgTask> untakedPickTaskList = this.prepkgPkgTaskService.loadTaskByParam(param);
			model.addAttribute("untakedPickTaskList", untakedPickTaskList);
			model.addAttribute("_warehouseCode", warehouseCode);
			model.addAttribute("_customerCode", customerCode);
			
			//初始化仓库数据
			if(StringUtils.isNotBlank(customerCode)){
				List<AreaGroup> warehouseList=this.areaGroupService.loadAreaGroupByAuthority(curUser,customerCode);
				model.addAttribute("warehouseList", warehouseList);
			}
		
		} catch (Exception e) {
			
			logger.error("error",e);
		}
		return "work/prepkg/prepkgPkgTaskList";
	}
	
	/**
	 * 领取包装任务
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "ajaxTakePkgTask")
	@ResponseBody
	public String ajaxTakePkgTask(ServletRequest request,HttpServletResponse response) {
		try {	
			Long taskId=Long.valueOf(request.getParameter("taskId")+""); 
			ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			this.prepkgPkgTaskService.takeTask(taskId,curUser.jobuuid);
			return "true";
		} catch (Exception e) {
			logger.error("error",e);
			return "false";
		}
	}
	
	/**
	 * 导向包装页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "toPkgTaskPage/{taskId}", method = RequestMethod.GET)
	public String toPkgTaskPage(@PathVariable("taskId") Long taskId, Model model) {
		try {

			//查询任务
			PrepkgPkgTask  task=this.prepkgPkgTaskService.loadTaskById(taskId);
		    model.addAttribute("task", task);
		    
		    //查询任务明细
		    List<PrepkgPkgItem> itemList=this.prepkgPkgItemService.loadByTaskCode(task.getTaskCode());
		    model.addAttribute("itemList", itemList);
		    
		    return "work/prepkg/prepkgPkgTask";
			
		} catch (Exception e) {
			
			logger.error("error",e);
		}
		return null;
	}
	
	/**
	 * 完成包装任务
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "ajaxCompletePkgTask")
	@ResponseBody
	public String ajaxCompletePkgTask(ServletRequest request,HttpServletResponse response) {
		try {	
			Long taskId=Long.valueOf(request.getParameter("taskId")+""); 
			this.prepkgPkgTaskService.completeTask(taskId);
			return "true";
		} catch (Exception e) {
			logger.error("error",e);
			return "false";
		}
	}
	
	
}
